import {createRouter, createWebHistory} from 'vue-router'
import LoginView from "@/view/LoginView.vue";
import WelcomeView from "@/view/WelcomeView.vue";
import IndexView from "@/view/user/IndexView.vue";
import {TOKEN_NAME} from "@/util/constants.js";
import {showMessage} from "@/util/message.js";

const router = createRouter({
    history: createWebHistory(),
    routes: [
        {
            path: '/',
            component: LoginView
        },
        {
            path: '/index',
            component: IndexView,
            meta: {requiresAuth: true},
            children: [
                {
                    path: '',
                    component: WelcomeView
                },
                /*商户信息操作*/
                {
                    path: 'user/list',
                    component: () => import('@/view/user/UserListView.vue')
                },
                {
                    path: 'user/edit',
                    component: () => import('@/view/user/UserEditOrAddView.vue')
                },
                {
                    path: 'user/edit/:userNo',
                    component: () => import('@/view/user/UserEditOrAddView.vue')
                },
                /*支付产品操作*/
                {
                    path: 'pay/product/list',
                    component: () => import('@/view/product/ProductView.vue')
                },
                {
                    path: 'pay/way/list',
                    component: () => import('@/view/payway/PayWayView.vue')
                },
                {
                    path: 'pay/way/add',
                    component: () => import('@/view/payway/PayWayAddView.vue')
                },
                /*商户支付配置操作*/
                {
                    path: 'pay/config/list',
                    component: () => import('@/view/payconfig/PayConfigView.vue')
                },
                {
                    path: 'pay/config/add',
                    component: () => import('@/view/payconfig/AddUserPayConfigView.vue')
                },
            ]
        },
    ]
})
router.beforeEach((to, from, next) => {
    const requiresAuth = to.matched.some(record => record.meta.requiresAuth);
    const token = sessionStorage.getItem(TOKEN_NAME);
    const token2 = localStorage.getItem(TOKEN_NAME);
    if (requiresAuth && !token && !token2) {
        showMessage('请先登录后再试', 'warning')
        next('/');
    } else {
        next();
    }
});
export default router;